﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using TracyGUI.View;
using SharedLib.WinFormMVCFramework;
using Tracy;
using SharedLib;
using TracyGUI.Model;
using System.Xml;
using SharedLib.Helpers;
using Tracy.DataModel.Config;

namespace TracyGUI.Controller
{
    class TracyConfigViewController : ControllerBase<ITracyConfigView>, IController<ITracyConfigView>, IHasViewState
    {
        protected override ITracyConfigView ResolveView()
        {
            return Tracy.TracyFacade.Instance.DIContainer.Resolve<ITracyConfigView>();
        }

        public TracyConfigViewController()
        {
            View.AutoDownloadModeSwitched += new EventHandler(View_AutoDownloadModeSwitched);
            TracyFacade.Instance.DownloadManager.AutoDownloadModeChanged += new EventHandler<GenericEventArgs<bool>>(DownloadManager_AutoDownloadModeChanged);
            //TracyFacade.Instance.DownloadManager.ConfigurablePluginFound += new EventHandler<GenericEventArgs<Tracy.DataModel.Config.IConfigurable>>(DownloadManager_ConfigurablePluginFound);
            InitConfiguablePlugins();
        }

        void InitConfiguablePlugins()
        {
            foreach (IConfigurable plugin in TracyFacade.Instance.ConfigurationManager.ManagedItems)
            {
                View.AddConfigMenu(plugin.Name, (innerSender, innerE) =>
                {
                    IConfigView view = plugin.CreateConfigViewController().View;
                    if (view != null && view is IFormView)
                    {
                        ((IFormView)view).ShowModal();
                    }
                });
            }
        }

        #region View Events
        void View_AutoDownloadModeSwitched(object sender, EventArgs e)
        {
            TracyFacade.Instance.ExceptionManager.Process(() =>
            {
                TracyFacade.Instance.DownloadManager.IsAutoDownloadMode = !TracyFacade.Instance.DownloadManager.IsAutoDownloadMode;
            }, "UI Policy");
        }
        #endregion
        
        void DownloadManager_AutoDownloadModeChanged(object sender, GenericEventArgs<bool> e)
        {
            View.DisplayAutoDownloadMode(e.Data ? TracyFacade.Instance.LanguageManager.GetText("AutoModeOn") : TracyFacade.Instance.LanguageManager.GetText("AutoModeOff"));
        }

        #region IHasViewState Members

        public void LoadViewState(ViewStateManager manager, XmlElement viewStateNode)
        {
            if (XmlHelper.GetNodeValue(viewStateNode, "auto-download") == "on")
            {
                TracyFacade.Instance.DownloadManager.IsAutoDownloadMode = true;
            }
            else
            {
                TracyFacade.Instance.DownloadManager.IsAutoDownloadMode = false;
            }
        }

        public void SaveViewState(ViewStateManager manager, XmlElement viewStateNode, XmlDocument doc)
        {
            viewStateNode.AppendChild(XmlHelper.CreateElementWithTextValue("auto-download", TracyFacade.Instance.DownloadManager.IsAutoDownloadMode ? "on" : "off", doc));
        }

        #endregion
    }
}
